﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.SECURITY.BLL;

namespace KPIS.GERP.SECURITY.App_UI.Forms
{
    public partial class Roles : System.Web.UI.Page
    {
        private RoleBLL roleBLL = new RoleBLL();
        private PermissionBLL perBLL = new PermissionBLL();
        private UserBLL userBLL = new UserBLL();
        private UserRoleBLL urBLL = new UserRoleBLL();
        private SsMng ssMng = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["EmpSeq"] = "60";
                Session["SYSTEMID"] = "GBMS";
                lblHeader.Text = "สิทธิ์การใช้งานระบบ " + rblSystemID.SelectedItem.Text;
                OnUpdateCmdUI("VIEW");
            }   
        }
        public void LoadDataGrid()
        {
            string SystemID = rblSystemID.SelectedValue;

            DataTable dt = roleBLL.LoadRoles((int?)null, SystemID);

            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
        public void LoadDataGridPermission()
        {
            string SystemID = rblSystemID.SelectedValue;

            DataTable dt = perBLL.LoadPermission(null
                , String.IsNullOrEmpty(lblRoleSeq.Text) ? (int?)null : Int32.Parse(lblRoleSeq.Text)
                , Convert.ToString(Session["SYSTEMID"]));

            RadGrid2.DataSource = dt;
            RadGrid2.DataBind();
        }
        public void LoadDataGridUser()
        {
            Nullable<int> RoleSequence = string.IsNullOrEmpty(lblRoleSeq.Text) ? (int?)null : Int32.Parse(lblRoleSeq.Text);

            DataSet ds = userBLL.LoadUserRole((int?)null, (int?)null, RoleSequence, "");
            DataTable dt = null;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                RadGrid3.DataSource = dt;
                RadGrid3.DataBind();
            }
        }
        private void OnUpdateCmdUI(string Mode)
        {
            bool bViewMode = Mode == "VIEW";
            bool bAddNewMode = Mode == "ADDNEW";
            bool bEditMode = Mode == "EDIT";
            bool bShowMode = Mode == "SHOW";
            bool bUserMode = Mode == "USER";

            PanelDetail.Visible = bShowMode;
            RadGrid1.Visible = bViewMode;
            PanelUser.Visible = bUserMode;
            RadGrid2.Visible = bShowMode;

            if (bViewMode)
            {
                LoadDataGrid();
            }
            else if (bShowMode)
            {
                LoadDataGridPermission();
            }
            else if (bUserMode)
            {
                LoadDataGridUser();
            }
        }
        public string GetTrueFalseImage(string strCompare, string strTrue, string strFalse
            , string strTrueImageUrl, string strFalseImageUrl)
        {
            if (strCompare == strTrue)
                return strTrueImageUrl;
            else if (strCompare == strFalse)
                return strFalseImageUrl;
            else
                return null;
        }
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int Sequence = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["role_seq"]);

                roleBLL.Delete(Sequence, ssMng.EmpSeqI);// Int32.Parse(Session["EmpSeq"].ToString()));

            }
            else if (e.CommandName == "ShowDetail" && e.Item is GridDataItem)
            {
                lblRoleSeq.Text = RadGrid1.Items[e.Item.ItemIndex]["role_seq"].Text;
                lblSystemName.Text = RadGrid1.Items[e.Item.ItemIndex]["system_id"].Text;
                cmdBackHome.Text = RadGrid1.Items[e.Item.ItemIndex]["role_name"].Text;

                OnUpdateCmdUI("SHOW");
            }
            else if (e.CommandName == "AddUser" && e.Item is GridDataItem)
            {
                lblRoleSeq.Text = RadGrid1.Items[e.Item.ItemIndex]["role_seq"].Text;
                lblSystemName.Text = RadGrid1.Items[e.Item.ItemIndex]["system_id"].Text;
                cmdBackFromUser.Text = RadGrid1.Items[e.Item.ItemIndex]["role_name"].Text;

                OnUpdateCmdUI("USER");
            }
            LoadDataGrid();
        }
        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int Sequence = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["role_seq"]);

                roleBLL.Delete(Sequence, ssMng.EmpSeqI);// Int32.Parse(Session["EmpSeq"].ToString()));

            }
            else if (e.CommandName == "EditView")// คลิกแก้ไข
            {
                GridDataItem item;
                RadGrid2.MasterTableView.Controls[0].Controls[0].Controls[0].FindControl("cmdEditView").Visible = false;
                RadGrid2.MasterTableView.Controls[0].Controls[0].Controls[0].FindControl("cmdUpdateALL").Visible = true;
                RadGrid2.MasterTableView.Controls[0].Controls[0].Controls[0].FindControl("cmdCancelALL").Visible = true;

                for (int i = 0; i < RadGrid2.Items.Count; i++)
                {
                    item = RadGrid2.Items[i];

                    RadioButton rdoN = (RadioButton)item["per_non"].FindControl("rdoN");
                    RadioButton rdoW = (RadioButton)item["per_write"].FindControl("rdoW");
                    RadioButton rdoR = (RadioButton)item["per_read"].FindControl("rdoR");

                    Image imgN = (Image)item["per_non"].FindControl("imgN");
                    Image imgW = (Image)item["per_write"].FindControl("imgW");
                    Image imgR = (Image)item["per_read"].FindControl("imgR");

                    rdoN.Visible = true;
                    rdoW.Visible = true;
                    rdoR.Visible = true;

                    imgN.Visible = false;
                    imgW.Visible = false;
                    imgR.Visible = false;
                }

                return;
            }
            else if (e.CommandName == "UpdateAll")// คลิกบันทึก
            {
                GridDataItem item;
                string PermissionType = "";
                string PermissionSeq = "";

                for (int i = 0; i < RadGrid2.Items.Count; i++)
                {
                    item = RadGrid2.Items[i];

                    RadioButton rdoN = (RadioButton)item["per_non"].FindControl("rdoN");
                    RadioButton rdoW = (RadioButton)item["per_write"].FindControl("rdoW");
                    RadioButton rdoR = (RadioButton)item["per_read"].FindControl("rdoR");

                    PermissionSeq = item["permission_seq"].Text.Replace("&nbsp;", "");

                    if(rdoR.Checked)
                        PermissionType = "R";
                    else if(rdoW.Checked)
                        PermissionType = "W";
                    else
                        PermissionType = "N";

                    if (string.IsNullOrEmpty(PermissionSeq))
                    {
                        perBLL.Insert(Int32.Parse(lblRoleSeq.Text)
                                      , Int32.Parse(item["menu_item_seq"].Text)
                                      , PermissionType
                                      , ssMng.EmpSeqI);// Int32.Parse(Session["EmpSeq"].ToString()));
                    }
                    else
                    {
                        perBLL.Update(Int32.Parse(PermissionSeq)
                            , Int32.Parse(lblRoleSeq.Text)
                            , Int32.Parse(item["menu_item_seq"].Text)
                            , PermissionType
                            , ssMng.EmpSeqI);// Int32.Parse(Session["EmpSeq"].ToString()));
                    }
                }

                RadGrid2.MasterTableView.Controls[0].Controls[0].Controls[0].FindControl("cmdEditView").Visible = true;
                RadGrid2.MasterTableView.Controls[0].Controls[0].Controls[0].FindControl("cmdUpdateALL").Visible = false;
                RadGrid2.MasterTableView.Controls[0].Controls[0].Controls[0].FindControl("cmdCancelALL").Visible = false;
            }
            else if (e.CommandName == "CancelAll")// คลิกยกเลิก
            {
                RadGrid2.MasterTableView.Controls[0].Controls[0].Controls[0].FindControl("cmdEditView").Visible = true;                
                RadGrid2.MasterTableView.Controls[0].Controls[0].Controls[0].FindControl("cmdUpdateALL").Visible = false;
                RadGrid2.MasterTableView.Controls[0].Controls[0].Controls[0].FindControl("cmdCancelALL").Visible = false;
                LoadDataGridPermission();
            }
            LoadDataGridPermission();
        }
        protected void rblSystemID_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblHeader.Text = "สิทธิ์การใช้งานระบบ " + rblSystemID.SelectedItem.Text;
            Session["SYSTEMID"] = rblSystemID.SelectedValue;
            //OnUpdateCmdUI("SHOW");
            LoadDataGrid();
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = (GridDataItem)e.Item;

                RadioButton rdoN = (RadioButton)dataItem["per_non"].FindControl("rdoN");
                RadioButton rdoW = (RadioButton)dataItem["per_write"].FindControl("rdoW");
                RadioButton rdoR = (RadioButton)dataItem["per_read"].FindControl("rdoR");

                string PermissionType = dataItem["permission_type"].Text;

                rdoN.Checked = PermissionType != "W" && PermissionType != "R";
                rdoW.Checked = PermissionType == "W";
                rdoR.Checked = PermissionType == "R";

                rdoN.Visible = false;
                rdoW.Visible = false;
                rdoR.Visible = false;

            }
        }

        protected void cmdBackHome_Click(object sender, EventArgs e)
        {
            OnUpdateCmdUI("VIEW");
        }

        protected void RadGrid3_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int Sequence = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["user_seq"]);
                //string UserRollSeq = RadGrid1.Items[e.Item.ItemIndex]["role_seq"].Text;

                userBLL.Delete(Sequence, ssMng.EmpSeqI);// Int32.Parse(Session["EmpSeq"].ToString()));
            }
            else if (e.CommandName == "InitInsertUser")
            {
                RadGrid3.MasterTableView.IsItemInserted = true;
            }
            LoadDataGridUser();
        }

    }
}
